package com.example.shop.common.controller;

import com.example.shop.common.api.ResponseResult;
import com.example.shop.common.api.ServiceStateCode;
import com.example.shop.common.bo.maplocation.InverseLocationInfo;
import com.example.shop.common.bo.maplocation.Location;
import com.example.shop.common.bo.maplocation.LocationInfo;
import com.example.shop.common.service.MapLocationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/common/map/location")
public class MapLocationController {

    @Autowired
    MapLocationService mapLocationService;

    @GetMapping(value = "/{area}",consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseResult<InverseLocationInfo> getInverseLocationInfo(@PathVariable("area") String area){
        InverseLocationInfo inverseLocationInfo = mapLocationService.getLocationInfo(area);
        if(inverseLocationInfo.getLevel().isDifficultLocate()){
            return ResponseResult.failure(ServiceStateCode.DIFFICULT_LOCATE);
        }
        if(!inverseLocationInfo.HighUnderstandPractical()){
            return ResponseResult.failure(ServiceStateCode.CANT_UNDERSTAND_LOCATE);
        }
        return ResponseResult.success(inverseLocationInfo);
    }

    @GetMapping(value = "/{lat}/{lng}",consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseResult<LocationInfo> getLocationInfo(@PathVariable("lat")double lat,@PathVariable("lng")double lng){
        LocationInfo locationInfo = mapLocationService.getLocationInfo(new Location(lat,lng));
        return ResponseResult.success(locationInfo);
    }
}
